package com.ndp.fb.exception;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Locale;
import java.util.ResourceBundle;

/**
 * Created by infi.he on 2016/1/26.
 */
public class ResourceNotificationMsgManager extends AbstractResourceManager {

    public static final String CN = "cn";
    public static final String US = "us";

    /**
     * 汉语资源包
     */
    public static ResourceBundle cnBundle = ResourceBundle.getBundle("notification_message", Locale.SIMPLIFIED_CHINESE);

    /**
     * 英文资源包
     */
    public static ResourceBundle usBundle = ResourceBundle.getBundle("notification_message", Locale.US);

    /**
     * 从eventValues
     *
     * @param language
     * @param eventKey
     * @param eventValues
     * @return
     */
    public static String getNotificationResource(String language, String eventKey, String eventValues) {
        ResourceBundle resourceBundle = getResourceBundle(language);
        String body;
        if (null == eventValues || eventValues.length() == 0 || "null".equalsIgnoreCase(eventValues)) {
            body = getResource(resourceBundle, eventKey);
        } else {
            JSONObject jsonObject = JSON.parseObject(eventValues);
            if (ResourceNotificationMsgManager.isCN(language)) {
                JSONArray cn = jsonObject.getJSONArray(ResourceNotificationMsgManager.CN);
                body = getResource(resourceBundle, eventKey, cn.toArray());
            } else {
                JSONArray us = jsonObject.getJSONArray(ResourceNotificationMsgManager.US);
                body = getResource(resourceBundle, eventKey, us.toArray());
            }
        }
        return body;
    }

    /**
     * 获取资源文件
     *
     * @param language
     * @return
     */
    private static ResourceBundle getResourceBundle(String language) {
        return isCN(language) ? cnBundle : usBundle;
    }
}
